import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutterlogistics/config/gloab_config.dart';
import 'package:flutterlogistics/config/resource_manager.dart';
import 'package:flutterlogistics/config/router_config.dart';
import 'package:flutterlogistics/utils/log_util.dart';
import 'package:flutterlogistics/utils/sp_util.dart';
import 'package:flutterlogistics/utils/timer_util.dart';

class SplashPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new SplashPageState();
  }
}

class SplashPageState extends State<SplashPage> {
  int _count = 2;
  TimerUtil _timerUtil;

  @override
  void initState() {
    super.initState();
    _initAsync();
  }

  void _initAsync() async {
    await SpUtil.getInstance();
    _doCountDown();
  }

  void _goPage() {
    if (SpUtil.getBool(GloabConfig.isGuide, defValue: true)) {
      LogUtil.e("去引导页");
      Navigator.of(context).pushReplacementNamed(routeGuide);
    } else if (SpUtil.getBool(GloabConfig.isLogin, defValue: false)) {
      // 去主页面
      LogUtil.e("去主页面");
      Navigator.of(context).pushReplacementNamed(routeHome);
    } else {
      // 登录页面
      LogUtil.e("去登录页面");
      Navigator.of(context).pushReplacementNamed(routeLogin);
    }
    cancelTimer();
  }

  void _doCountDown() {
    _timerUtil = new TimerUtil(mTotalTime: 2 * 1000);
    _timerUtil.setOnTimerTickCallback((int tick) {
      double _tick = tick / 1000;
      setState(() {
        _count = _tick.toInt();
      });
      if (_tick == 0) {
        _goPage();
      }
    });
    _timerUtil.startCountDown();
  }

  // 跳过
  Widget _jumpToPage() {
    return InkWell(
      onTap: () => _goPage(),
      child: Container(
        alignment: Alignment.topRight,
        margin: EdgeInsets.only(right: 60.w, top: 100.h),
        child: Container(
          padding: EdgeInsets.all(20.w),
          child: new Text(
            '跳过 $_count',
            style: new TextStyle(fontSize: 28.sp, color: Colors.white),
          ),
          decoration: BoxDecoration(
            color: Color(0x66000000),
            borderRadius: BorderRadius.all(Radius.circular(4.w)),
            border: new Border.all(width: 1.w, color: Colors.white30),
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context,width: 750,height: 1330);
    return Material(
      child: Stack(
        children: <Widget>[
          Container(
            alignment: Alignment.center,
            decoration: BoxDecoration(
                image: DecorationImage(
              image: AssetImage(ResourceManager.wrapAssets('launcher_bg_guide.jpg')),
              fit: BoxFit.cover,
            )),
            width: double.infinity,
            height: double.infinity,
//            child: Row(
//              mainAxisAlignment: MainAxisAlignment.end,
//              children: <Widget>[_jumpToPage()],
//            ),
          )
        ],
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    cancelTimer(); //timer cancel。
  }

  void cancelTimer() {
    if (_timerUtil != null) _timerUtil.cancel();
  }
}
